package com.bsoft.gol.hbs.service.impl;

import com.bsoft.gol.hbs.bean.enums.ArticleStatusEnum;
import com.bsoft.gol.hbs.bean.qo.MyArticleListQo;
import com.bsoft.gol.hbs.dao.HealthInfoDAO;
import com.bsoft.gol.hbs.utils.BeanUtils;
import com.bsoft.gol.hbs.utils.CommonUtils;
import com.bsoft.gol.hbs.utils.Constants;
import ctd.spring.boot.annotation.SsdevService;
import hcn.base.healthact.HealthActivitiesService;
import hcn.base.healthact.vo.HealthActivitiesDetailVO;
import hcn.base.healthinfo.HealthInfo;
import hcn.base.healthinfo.HealthInfoNoAuthService;
import hcn.base.healthinfo.HealthInfoService;
import hcn.base.healthinfo.qo.ArticleListRequest;
import hcn.base.healthinfo.vo.ClientArticleDetailVO;
import hcn.base.healthinfo.vo.MyArticleListVO;
import org.atag.dao.model.SimplePageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 不需要登录的健康资讯服务
 *
 * @author linyi
 * @date 2021/11/1 16:52
 */
@SsdevService(value = "healthNewsNoAuth",auth = false)
public class HealthInfoNoAuthServiceImpl implements HealthInfoNoAuthService {


    @Autowired
    private HealthInfoDAO healthInfoDAO;

    @Autowired
    private HealthInfoService healthNews;

    @Autowired
    private HealthActivitiesService healthActivities;

    @Override
    public SimplePageInfo<MyArticleListVO> articleList(ArticleListRequest request) {
        if (null != request.getCategory() && Constants.ZERO == request.getCategory()) {
            request.setCategory(null);
        }

        MyArticleListQo qo = new MyArticleListQo();
        org.springframework.beans.BeanUtils.copyProperties(request, qo);
        qo.setArticleStatus(ArticleStatusEnum.RELEASED.getState());
        qo.setDesc(true);
        SimplePageInfo<HealthInfo> healthInfoList;

        healthInfoList = healthInfoDAO.getHealthInfoPage(qo);

        SimplePageInfo<MyArticleListVO> pageInfo = new SimplePageInfo<>();
        pageInfo.setTotal(healthInfoList.getTotal());
        if (CommonUtils.isNotEmpty(healthInfoList) && CommonUtils.isNotEmpty(healthInfoList.getList())) {
            List<MyArticleListVO> collect = healthInfoList.getList().stream()
                    .map(info -> BeanUtils.copyProperties(info, MyArticleListVO.class))
                    .collect(Collectors.toList());
            pageInfo.setList(collect);
        } else {
            pageInfo.setList(Collections.emptyList());
        }
        return pageInfo;
    }

    /**
     * 客户端文章详情
     */
    @Override
    public ClientArticleDetailVO clientArticleDetail(String articleId) {
        return  healthNews.clientArticleDetail(articleId);
    }

    /**
     * 不需要token查看、记录阅读量
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public HealthActivitiesDetailVO detailInfoForH5(Integer id) throws Exception {
        HealthActivitiesDetailVO healthActivitiesDetailVO = healthActivities.detailInfoForH5(id);
        return healthActivitiesDetailVO;
    }
}
